<?php
/*
说明：
需求参数--$quizid：在mdl_quiz表中某个测试的id
返回--$status：false/true
	 $msg：提示信息
	 $data：数组，内含测试题目及选项（仅限单选题目）

*/

session_start();//session开始标签，在最顶部写。
/*---------------------一、PHP文件引用----------------------------------*/
require_once 'response2.php';
require_once 'connect_config.php';
require('../config.php');

/*---------------------二、数据库连接--------------------------------*/
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PORT) or die('数据库连接失败');
mysqli_query($conn,"SET NAMES utf8");

/*---------------------三、变量定义--------------------------------*/
$status = false;
$msg = '';
$data = array();

$quizid=$_GET['quizid'];
$token=$_GET['token'];
/*
//测试数据
$quizid=2;
$token='f47ad7d1e02be1156bf70e786c25cec5';
*/
if(!$token)
{
	$username=$_SESSION["username"];	
}
$serverpath=$_SERVER['SERVER_ADDR'];//服务器IP 
$i=0;
/*---------------四、用户ID（确保登录用户才能获取资源）------------------*/	
if($token)
{
	$query1 = "select * from mdl_token where token='$token'";
	$data1= mysqli_query($conn, $query1);
	if($row1 = mysqli_fetch_array($data1))
	{
		$userid=$row1['user_id'];
		//echo "userid success<br>";
	}
	else
	{
		$status = false;
		$msg = 'cannot find userid in mdl_token';
		echo Response::json($status,$msg,$data);
	}
}
else if($username)
{
	$query1 = "select * from mdl_user where username='$username'";
	$data1= mysqli_query($conn, $query1);
	if($row1 = mysqli_fetch_array($data1))
	{
		$userid=$row1['id'];
		//echo "userid success<br>";
	}
	else
	{
		$status = false;
		$msg = 'cannot find userid in mdl_token';
		echo Response::json($status,$msg,$data);
	}
}
/*---------------五、获取测试内容------------------*/	
if($userid)
{
	$query2 = "select * from mdl_quiz_slots where quizid='$quizid'";
	$data2= mysqli_query($conn, $query2);
	while($row2 = mysqli_fetch_array($data2))//找到指定的quizid对应的questionid
	{
		$id=$row2['questionid'];
		$query3 = "select * from mdl_question where id='$id'";
		$data3= mysqli_query($conn, $query3);
		$row3 = mysqli_fetch_array($data3);
		if($row3['qtype']=='multichoice')//只录入选择题
		{
			
			$arr=array('<b>','<p>','<br>','<sub>','<u>','<i>','<strike>','<sup>','&nbsp;','</p>','</b>','</br>','</sub>','</u>','</i>','</strike>','</sup>');
		//	$data[$i]['question-content']=$row3['questiontext'];
			$data[$i]['questionname']=$row3['name'];//题目名称
			$data[$i]['questioncontent']=str_replace($arr,"",$row3['questiontext']);//题目内容
			$query4 = "select * from mdl_question_answers where question='$id'";
			$data4= mysqli_query($conn, $query4);
			$temp=array();
            $j=0;
			while($row4 = mysqli_fetch_array($data4))
			{
                $row4['answer']=str_replace($arr,"",$row4['answer']);
                $temp[$j]['answer']=$row4['answer'];
                $temp[$j]['answerid']=$row4['id'];
                $j++;
			}
            $data[$i]['options']=$temp;
			$data[$i]['score']=$row2['maxmark'];//题目分值
			$i++;
		}
		else
		{
			continue;	
		}		
	}
	$count=$i;//统计试题数量
	if(count($data)>0)
	{
		$status = true;
		$msg = "there is ".$count." questions in quiz<br>";
		echo Response::json($status,$msg,$data);
	}
	else
	{
		$status = false;
		$msg = "cannot get quiz questions from mdl_question";
		echo Response::json($status,$msg,$data);	
	}
	
}

?>
